﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace EZBarTender_Wrapper.SmallLabels
{
	public class SmallLabelDataAccess
	{
		public static DataTable GetSmallLabelBatches(string connectionString)
		{
			DataTable table = new DataTable();
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("EXECUTE sp_GetSmallLabelBatches", connection))
				{
					using (SqlDataAdapter adapter = new SqlDataAdapter(command))
					{
						try
						{
							adapter.Fill(table);
						}
						catch
						{
							throw;
						}
					}
				}
				connection.Close();
			}
			return table;
		}

		public static void SetSmallLabels(string connectionString, string batchNo)
		{
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();

				using (SqlCommand command = new SqlCommand("EXECUTE sp_SetSmallLabel @batch",
					connection))
				{
					command.Parameters.AddWithValue("@batch", batchNo);
					try
					{
						command.ExecuteNonQuery();
					}
					catch
					{
						throw;
					}
				}
				connection.Close();
			}
		}

		public static void UnetSmallLabels(string connectionString, string batchNo)
		{
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();

				using (SqlCommand command = new SqlCommand("EXECUTE sp_UnsetSmallLabel @batch",
					connection))
				{
					command.Parameters.AddWithValue("@batch", batchNo);
					try
					{
						command.ExecuteNonQuery();
					}
					catch
					{
						throw;
					}
				}

				connection.Close();
			}
		}

		public static void ChangeSmallLabel(string connectionString, string upc, string flag)
		{
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				string commandString = "EXECUTE sp_ChangeSmallLabel @Upc";
				commandString += string.IsNullOrEmpty(flag) ? "" : ", @Flag";
				using (SqlCommand command = new SqlCommand(commandString, connection))
				{
					command.Parameters.AddWithValue("@Upc", upc);
					if(!string.IsNullOrEmpty(flag))
						command.Parameters.AddWithValue("@Flag", flag);

					try
					{
						command.ExecuteNonQuery();
					}
					catch
					{
						throw;
					}
				}
				connection.Close();
			}
		}

		public static string GetSmallLabelStatus(string connectionString, string upc)
		{
			string result = "";
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("EXECUTE sp_GetCurrentSmallValue @Upc", connection))
				{
					command.Parameters.AddWithValue("@Upc", upc);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						if (reader.Read())
						{
							result = reader["LabelWidth"].ToString();
						}
						reader.Close();
					}
				}
				connection.Close();
			}
			return result;
		}
	}
}
